package com.zhoug.common.validator;

import com.zhoug.common.utils.JsonUtils;
import com.zhoug.common.utils.LogUtils;
import com.zhoug.common.utils.StringUtils;
import com.zhoug.common.validator.annotation.ValidateLength;
import com.zhoug.common.validator.annotation.ValidateName;
import com.zhoug.common.validator.annotation.ValidateNull;

import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.PrimitiveIterator;

import androidx.annotation.NonNull;


/**
 * @Author: zhoug
 * @Date: 2022/9/23
 * @Description:
 */
public class Validator {
    private static final String TAG = ">>>Validator";

    public static ValidateResult validate(@NonNull Object data) {
        ValidateResult validateResult=new ValidateResult();
        Class<?> cls = data.getClass();
        //验证Filed
        List<String> result = new ArrayList<>();
        boolean success=true;
        Field[] declaredFields = cls.getDeclaredFields();
        for (Field field : declaredFields) {
            String fieldName = field.getName();
            Object fieldValue = getValue(field, data);
            LogUtils.d(TAG, fieldName + ":" + fieldValue);
            //属性描述
            String describeName = null;
            ValidateName validateName = field.getAnnotation(ValidateName.class);
            if (null != validateName) {
                describeName = validateName.value();
            }
            if (StringUtils.isNullOrEmpty(describeName)) {
                describeName = fieldName;
            }
            //非空验证
            ValidateNull validateNull = field.getAnnotation(ValidateNull.class);
            if (null != validateNull) {
                String error = StringUtils.isNullOrEmpty(validateNull.describe()) ? describeName + "不能为空" : validateNull.describe();
                if (null == fieldValue) {
                    success=false;
                    result.add(error);
                }else if(!validateNull.allowEmpty()){
                  //不允许为空
                  if(fieldValue.toString().length()==0){
                      result.add(error);
                  }else {
                      if(fieldValue instanceof Collection){
                          if(((Collection<?>) fieldValue).size()==0){
                              result.add(error);
                          }
                      }else if(fieldValue instanceof Map){
                          if(((Map<?,?>) fieldValue).size()==0){
                              result.add(error);
                          }
                      }
                  }
                }
            }
            //字符串长度验证
            ValidateLength validateLength = field.getAnnotation(ValidateLength.class);
            if(null != validateLength){
                int min = validateLength.min();
                int max = validateLength.max();
                if(fieldValue instanceof String){
                    int len=((String) fieldValue).length();
                }

            }



            field.setAccessible(false);
        }
        validateResult.setMsg(JsonUtils.toJson(result));
        validateResult.setSuccess(success);
        return validateResult;
    }



    private static Object getValue(Field field, Object obj) {
        try {
            field.setAccessible(true);
            Object value = field.get(obj);
            field.setAccessible(false);
            return value;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }


}
